// 这道题, 就是依赖关系的简单逻辑题
// 首先根据依赖关系, 确定有哪些服务, 以及服务之间的依赖关系
// 其次根据故障服务, 找被依赖服务, 全部改为故障状态, 可以使用
// 优先队列, 一轮轮的敲掉, 最后剩下的就是OK的
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let dependArr = [];
let errorArr = [];
rl.on("line", (line)=>{
    if(dependArr.length === 0) {
        dependArr = line.trim().split(",")
    } else if(errorArr.length === 0) {
        errorArr = line.trim().split(",")
        getResult(dependArr, errorArr)
        // clear
        dependArr.length = 0
        errorArr.length = 0
    }

})

function getResult(dependArr, errorArr) {
    let services = new Set()
    let depend = {}
    for (const str of dependArr) {
        const [a, b] = str.split("-")
        services.add(a)
        services.add(b)
        if(depend[b]) {
            depend[b].push(a)
        } else {
            depend[b] = [a]
        }
    
        if(!depend[a]) {
            depend[a] = []
        }
    }
    
    let pq = [...errorArr]
    while(pq.length) {
        let node = pq.shift()
        services.delete(node)
        
        let arr = depend[node]
        for (const child of arr) {
            pq.push(child)
        }
    }
    if(services.size > 0) {
        console.log(Array.from(services).join(","))
    } else {
        console.log(",")
    }
}